package com.wenlin.myproduction.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.wenlin.myproduction.R;

/**
 * @author 文琳
 * @time 2020/9/16 9:37
 * @desc 画个酷炫的logo
 */
public class LogoAnimView extends View {
    private int viewWidth = 488, viewHeight = 122;
    private Paint paintWhite, paintGreen;
    private Path path, path2;
    @ColorInt
    private int color;
    @ColorInt
    private int colorBg;
    private static final int ANIM_DURATION = 1000;
    private ValueAnimator animator;
    private int progress;//进度
    private LinearGradient gradient;

    public LogoAnimView(Context context) {
        this(context, null);
    }

    public LogoAnimView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LogoAnimView);
        color = typedArray.getColor(R.styleable.LogoAnimView_lav_color, Color.WHITE);
        colorBg = typedArray.getColor(R.styleable.LogoAnimView_lav_bg_color, Color.parseColor("#2fc989"));
        typedArray.recycle();
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        paintWhite = new Paint();
        paintWhite.setAntiAlias(true);
        paintWhite.setDither(true);
        paintWhite.setStyle(Paint.Style.FILL);
        paintWhite.setColor(color);
        paintGreen = new Paint();
        paintGreen.setAntiAlias(true);
        paintGreen.setDither(true);
        paintGreen.setStyle(Paint.Style.FILL);
        paintGreen.setColor(colorBg);
        path = new Path();
        path2 = new Path();
//        startAnim();
    }

    /**
     * 开始动画
     */
    public void startAnim() {
        if (animator != null) animator.cancel();
        animator = ValueAnimator.ofInt(0, 100);
        animator.addUpdateListener(animation -> {
            progress = (int) animation.getAnimatedValue();
            gradient = new LinearGradient((progress - 25) * viewWidth / 100f, 0, progress * viewWidth / 100f, 7,
                    color, Color.parseColor("#cccccc"), Shader.TileMode.MIRROR);
            paintWhite.setShader(gradient);
            invalidate();
        });
        animator.setDuration(ANIM_DURATION);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(colorBg);
        //画圆角矩形
        path2.moveTo(21, 0);
        path2.lineTo(135, 0);
        path2.quadTo(142, 0, 140, 7);
        path2.lineTo(126, 88);
        path2.quadTo(126, 93, 121, 93);
        path2.lineTo(6, 93);
        path2.quadTo(0, 93, 0, 88);
        path2.lineTo(15, 7);
        path2.quadTo(15, 0, 21, 0);
        path2.close();
        canvas.drawPath(path2, paintWhite);
        //画M
        path.moveTo(26, 10);
        path.lineTo(15, 65);
        path.lineTo(26, 65);
        path.lineTo(33, 20);
        path.lineTo(40, 65);
        path.lineTo(49, 65);
        path.lineTo(73, 20);
        path.lineTo(65, 65);
        path.lineTo(76, 65);
        path.lineTo(86, 10);
        path.lineTo(70, 10);
        path.lineTo(48, 51);
        path.lineTo(42, 10);
        path.close();
        canvas.drawPath(path, paintGreen);
        //画y
        path.reset();
        path.moveTo(87, 25);
        path.lineTo(95, 65);
        path.quadTo(90, 73, 85, 74);
        path.lineTo(79, 74);
        path.lineTo(78, 82);
        path.lineTo(85, 83);
        path.quadTo(95, 83, 103, 69);
        path.lineTo(127, 25);
        path.lineTo(117, 25);
        path.lineTo(102, 54);
        path.lineTo(99, 25);
        path.close();
        canvas.drawPath(path, paintGreen);
        //画E
        path.reset();
        path.moveTo(151, 5);
        path.lineTo(135, 92);
        path.lineTo(185, 92);
        path.lineTo(188, 77);
        path.lineTo(156, 77);
        path.lineTo(160, 56);
        path.lineTo(187, 56);
        path.lineTo(190, 41);
        path.lineTo(163, 41);
        path.lineTo(167, 20);
        path.lineTo(196, 20);
        path.lineTo(199, 5);
        path.close();
        canvas.drawPath(path, paintWhite);
        //画n
        path.reset();
        path.moveTo(202, 30);
        path.lineTo(191, 92);
        path.lineTo(209, 92);
        path.lineTo(215, 56);
        path.cubicTo(220, 38, 239, 37, 236, 58);
        path.lineTo(230, 92);
        path.lineTo(248, 92);
        path.lineTo(256, 49);
        path.cubicTo(261, 23, 223, 23, 218, 40);
        path.lineTo(220, 30);
        canvas.drawPath(path, paintWhite);
        //画g
        path.reset();
        path.moveTo(309, 37);
        path.quadTo(305, 27, 290, 29);
        path.cubicTo(255, 32, 243, 97, 278, 94);
        path.quadTo(293, 93, 300, 83);
        path.quadTo(293, 117, 257, 101);
        path.lineTo(254, 116);
        path.quadTo(306, 129, 315, 92);
        path.lineTo(303, 65);
        path.cubicTo(299, 88, 265, 87, 277, 57);
        path.cubicTo(284, 37, 312, 37, 303, 65);
        path.lineTo(315, 92);
        path.lineTo(327, 30);
        path.lineTo(310, 30);
        path.close();
        canvas.drawPath(path, paintWhite);
        //画l
        path.reset();
        path.moveTo(338, 5);
        path.lineTo(321, 92);
        path.lineTo(339, 92);
        path.lineTo(356, 5);
        path.close();
        canvas.drawPath(path, paintWhite);
        //画i
        path.reset();
        path.moveTo(357, 30);
        path.lineTo(346, 92);
        path.lineTo(364, 92);
        path.lineTo(375, 30);
        path.close();
        canvas.drawPath(path, paintWhite);
        path.reset();
        path.moveTo(377, 20);
        path.cubicTo(371, 29, 354, 23, 363, 9);
        path.cubicTo(370, 0, 385, 8, 377, 20);
        path.close();
        canvas.drawPath(path, paintWhite);
        //画s
        path.reset();
        path.moveTo(424, 31);
        path.quadTo(385, 22, 378, 47);
        path.quadTo(376, 62, 390, 66);
        path.cubicTo(418, 69, 398, 87, 374, 74);
        path.lineTo(371, 90);
        path.cubicTo(423, 105, 437, 57, 405, 54);
        path.cubicTo(382, 51, 397, 35, 421, 45);
        path.lineTo(424, 30);
        path.close();
        canvas.drawPath(path, paintWhite);
        //画h
        path.reset();
        path.moveTo(438, 5);
        path.lineTo(422, 92);
        path.lineTo(440, 92);
        path.lineTo(447, 56);
        path.cubicTo(452, 38, 471, 37, 468, 58);
        path.lineTo(462, 92);
        path.lineTo(480, 92);
        path.lineTo(488, 49);
        path.cubicTo(493, 23, 455, 23, 450, 40);
        path.lineTo(456, 5);
        path.close();
        canvas.drawPath(path, paintWhite);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(viewWidth, viewHeight);
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
